package com.winspread.app.viewpager

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter

/**
 *Author: Amin
 *Data: 2023/7/4 17:19
 *
 *Description:
 *
 */
class MyAdapter(var fragments: MutableMap<String, Fragment>, var fm: FragmentManager, var lifecycle: Lifecycle) :
    FragmentStateAdapter(fm, lifecycle) {
    override fun getItemCount(): Int {
        return fragments.size

    }

    override fun createFragment(position: Int): Fragment {
        return fragments.values.toMutableList()[position]
    }


    fun add(key: String, fragment: Fragment) {
        fragments[key] = fragment
        notifyDataSetChanged()
    }


    fun remove(key: String) {
        fragments.remove(key)
        notifyDataSetChanged()
    }


    override fun getItemId(position: Int): Long {
        return fragments.keys.toMutableList()[position].hashCode().toLong()
    }

    override fun containsItem(itemId: Long): Boolean {
        return super.containsItem(itemId)
    }


}